
/***Define Global Directory ****/
//Change rootdir to where store files
	global ROOTDIR "F:/Dropbox/national banks peg/Replication_files"
	local PROGDIR  "$ROOTDIR"
	local INDIR  "$ROOTDIR/data"
	local OUTDIR  "$ROOTDIR/data"
/*******************************/

set more off

cd "`INDIR'"

use  National_bank_call_reports_states, clear
 
gen ddate = call_date - l.call_date


keep call_date ddate


gen year = year(call_date)
gen month = month(call_date)

keep year month ddate

drop if ddate==.

duplicates drop


merge 1:m year month using "`INDIR'/indicators.dta"

gen date = ym(year,month)
tsset date


replace failures = . if failures_q<.


drop if year<=1880

gen ind = 1 if _n==1
tsset date




forval i = 2/360{

replace ind = l.ind if l._merge==2 & _n==`i'
replace ind = l.ind+1 if l._merge==3 & _n==`i'
}

gen yearc = year if _merge==3
gen monthc = month if _merge==3





gen lnfailures_q = ln(failures_q)
gen dlnfailures_q = d.lnfailures_q
egen dlnfailures_q_m = mean(dlnfailures_q), by(year quarter)

drop dlnfailures_q
rename dlnfailures_q_m dlnfailures_q

replace failures = failures_q/3*exp(dlnfailures_q) if failures==. & f.failures<.


xi: reg dlnfailures_q i.month

predict dlnfailures_q_sa, res

replace dlnfailures_q_sa = . if dlnfailures_q_sa==0

drop _I*

foreach var of varlist fact_emp-IP IPadj{
gen ln`var' = ln(`var')
gen dln`var' = d.ln`var'

xi: reg dln`var' i.month

predict dln`var'_sa, res

replace dln`var'_sa = . if `var'== 0 

}


replace dlnfailures_sa = dlnfailures_q_sa if failures_q<.

drop dlnfailures_q*



collapse (sum) yearc monthc dln*sa ddate (mean) gold_diff, by(ind) 

rename yearc year
rename monthc month

drop if year==.

replace gold_diff = 0 if gold_diff ==.
order gold_diff, before(ddate)
export excel using "y", replace
